package easymusic.baseutility;

import java.io.UnsupportedEncodingException;

import com.easymusic.EasyMusicToast;
import com.easymusic.R;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
import easymusic.baseinterface.ErrorCode;
import easymusic.baseinterface.IAppDataService;
import easymusic.baseinterface.IBasicService;
import easymusic.baseinterface.IMediaDataService;

class EasyMusicMediaDataService extends AbstractService implements IMediaDataService {
	class EasyMusicMediaData implements IMediaDataService.IMediaData{
		
		public String m_strPath;
		public String m_strMediaName;
		public int m_iDuration;
		public Bitmap m_bitmap;
		
		
		@Override
		public String getMediaName() {
			return m_strMediaName;
		}

		@Override
		public int getDuration() {
			return m_iDuration;
		}

		@Override
		public Bitmap getAlbum() {
			return m_bitmap;
		}
		
	}
	
	Cursor m_cursor;
	private Bitmap DEFAULT_BITMAP;
	
	public EasyMusicMediaDataService(){
		
	}
	
	@Override
	public void startService(EasyMusicAppService service, Context context) {
		
		DEFAULT_BITMAP = BitmapFactory.decodeResource(context.getResources(), R.drawable.easy_music_icon);
		// TODO Auto-generated method stub
		refreshMedia();
		// test
		Log.d("jacklog", "jacklog:  the number of media is " + getNumberOfMedia());
		for (moveToFirst(); !isEnd(); moveToNext()){
			IMediaData media = getMediaData();
			if (null == media){
				Log.d("jacklog", "jacklog: sorry cannot get media data!!");
				continue;
			}
			Log.d("jacklog", "jacklog: get a media: " + media.getMediaName());
		}
	}

	@Override
	public void stopService() {
	}

	@Override
	public int getNumberOfMedia() {
		if (null == m_cursor){
			return 0;
		}
		return m_cursor.getCount();
	}

	@Override
	public ErrorCode refreshMedia() {
		IAppDataService service = (IAppDataService)getService(IBasicService.ServiceID.AppDataService);
		Context context = (Context) service.getAppData(IAppDataService.APP_CONTEXT);
		ContentResolver resolver = context.getContentResolver();
		String[] projection = {MediaStore.Audio.Media.DATA, 
								MediaStore.Audio.Media.DURATION, 
								MediaStore.Audio.Media.ALBUM_ID,
								MediaStore.Audio.Media.TITLE};
		m_cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
		return ErrorCode.success;
	}

	@Override
	public void moveToFirst() {
		if (null == m_cursor){
			return;
		}
		m_cursor.moveToFirst();
	}

	@Override
	public IMediaData getMediaData() {
		if (null == m_cursor || m_cursor.isLast()){
			return null;
		}
		EasyMusicMediaData media = new EasyMusicMediaData();
		media.m_strPath = m_cursor.getString(m_cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
		media.m_iDuration = m_cursor.getInt(m_cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
		//try{
			// i cannot find a way to show Chinese correctively. so i just use the path name 
			//media.m_strMediaName = new String(m_cursor.getBlob(m_cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)), "GB2312");
			media.m_strMediaName = media.m_strPath.substring(media.m_strPath.lastIndexOf('/') + 1, media.m_strPath.lastIndexOf('.'));
		/*}catch(UnsupportedEncodingException e){
			IAppDataService service = (IAppDataService)getService(ServiceID.AppDataService);
			EasyMusicToast.show((Context)service.getAppData(IAppDataService.APP_CONTEXT), e.getMessage());
		}*/
		 
				
		IAppDataService service = (IAppDataService)getService(IBasicService.ServiceID.AppDataService);
		Context context = (Context) service.getAppData(IAppDataService.APP_CONTEXT);
		ContentResolver resolver = context.getContentResolver();
		String[] projection = {MediaStore.Audio.Albums.ALBUM_ART};
		int iID = m_cursor.getInt(m_cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
		Uri uri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
		uri = uri.parse(uri.toString() + "/" + iID);
		Cursor cursorAlbum = resolver.query(uri, projection, null, null, null);
		if (null == cursorAlbum || cursorAlbum.getCount() <= 0){
			media.m_bitmap = DEFAULT_BITMAP;
		}else{
			cursorAlbum.moveToNext();
			String strPath = cursorAlbum.getString(0);
			if (null != strPath){
				media.m_bitmap = BitmapFactory.decodeFile(strPath);
				Log.d("jacklog", "jacklog: great get a media bitmap, its media name is " + media.m_strMediaName);
			}else{
				media.m_bitmap = (Bitmap)service.getAppData(IAppDataService.APP_DEFAULT_ICON);
			}
		} 
		
		return media;
	}

	@Override
	public void moveToNext() {
		if (null != m_cursor){
			m_cursor.moveToNext();
		}
	}

	@Override
	public boolean isEnd() {
		if (null == m_cursor || m_cursor.isLast() || m_cursor.isClosed()){
			return true;
		}
		return false;
	}

	@Override
	public void moveToPosition(int iIndex){
		if (null == m_cursor || iIndex >= m_cursor.getCount()){
			return;
		}
		m_cursor.moveToPosition(iIndex);
	}

	@Override
	public String getPath() {
		if (null == m_cursor|| m_cursor.isLast() || m_cursor.isClosed()){
			return null;
		}
		return m_cursor.getString(m_cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
	}

	@Override
	public int getCurPos() {
		if (null == m_cursor|| m_cursor.isLast() || m_cursor.isClosed()){
			return -1;
		}
		
		return m_cursor.getPosition();
	}
}







































